% 6/11
% Based on SBY's findR
function R1 = findR(Q)
    %st = ftimer();
    global J I CURRENT fL1 FL1
    n = CURRENT;
    Q1 = Q;
    %FL1 = @function_F;
    %fL1 = @function_Poiss;

    R1=zeros(I,n+1);
    for m = 0:n 
        R1(1,1) = R1(1,1) + (1-FL1(m+1)) * Q1(1,m+1);
    end
    %计算R(1,2:n+1)
    for k = 1:n 
        for m=0:n        
            if m-k+1>0
              R1(1,k+1) = R1(1,k+1) + fL1(m-k+1)*Q1(1,m+1);
            end
        end
    end
    
    %求R1的第2到n行
    for i = 2:I
        %计算i行第1列
        for k2 = 0:n%此处k2指k~
            for m = 0:n   
              R1(i,1) = R1(i,1) + (1-FL1(m+k2+1))*Q1(i,m+1)*R1(i-1,k2+1);
            end
        end
      
      %计算i行第2到n+1列
      for k=1:n
          for m=0:n   
              for k2 = 0:n%注意这里的k2指k~
                  if m+k2-k+1 > 0   
                      R1(i,k+1) = R1(i,k+1)+fL1(m+k2-k+1)*Q1(i,m+1)*R1(i-1,k2+1);
                  end
              end
          end
      end

    end

    %global T_findR N_findR
    %T_findR = T_findR + ftimer()-st;
    %N_findR = N_findR + 1;
    return;

